<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Java concurrency</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Java_concurrency"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Java_concurrency rootpage-Java_concurrency skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Java concurrency</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<style data-mw-deduplicate="TemplateStyles:r1305433154">
/* start https://en.wikipedia.org/ */
.mw-parser-output .ambox{border:1px solid #a2a9b1;border-left:10px solid #36c;background-color:#fbfbfb;box-sizing:border-box}.mw-parser-output .ambox+link+.ambox,.mw-parser-output .ambox+link+style+.ambox,.mw-parser-output .ambox+link+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+style+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+link+.ambox{margin-top:-1px}html body.mediawiki .mw-parser-output .ambox.mbox-small-left{margin:4px 1em 4px 0;overflow:hidden;width:238px;border-collapse:collapse;font-size:88%;line-height:1.25em}.mw-parser-output .ambox-speedy{border-left:10px solid #b32424;background-color:#fee7e6}.mw-parser-output .ambox-delete{border-left:10px solid #b32424}.mw-parser-output .ambox-content{border-left:10px solid #f28500}.mw-parser-output .ambox-style{border-left:10px solid #fc3}.mw-parser-output .ambox-move{border-left:10px solid #9932cc}.mw-parser-output .ambox-protection{border-left:10px solid #a2a9b1}.mw-parser-output .ambox .mbox-text{border:none;padding:0.25em 0.5em;width:100%}.mw-parser-output .ambox .mbox-image{border:none;padding:2px 0 2px 0.5em;text-align:center}.mw-parser-output .ambox .mbox-imageright{border:none;padding:2px 0.5em 2px 0;text-align:center}.mw-parser-output .ambox .mbox-empty-cell{border:none;padding:0;width:1px}.mw-parser-output .ambox .mbox-image-div{width:52px}@media(min-width:720px){.mw-parser-output .ambox{margin:0 10%}}@media print{body.ns-0 .mw-parser-output .ambox{display:none!important}}
/* end https://en.wikipedia.org/ */
</style>
<p>The <a href="Java_(programming_language)" title="Java (programming language)">Java programming language</a> and the <a href="Java_virtual_machine" title="Java virtual machine">Java virtual machine</a> (JVM) are designed to support <a href="Concurrent_programming" class="mw-redirect" title="Concurrent programming">concurrent programming</a>. All execution takes place in the context of <a href="Thread_(computer_science)" class="mw-redirect" title="Thread (computer science)">threads</a>. Objects and resources can be accessed by many separate threads. Each thread has its own path of execution, but can potentially access any object in the program. The programmer must ensure read and write access to objects is properly coordinated (or "<a href="Synchronization_(computer_science)" title="Synchronization (computer science)">synchronized</a>") between threads.<sup id="cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer200615–17§2_Thread_Safety_1-0" class="reference"><a href="#cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer200615–17§2_Thread_Safety-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§11_Item_78:_Synchronize_access_to_shared_mutable_data_2-0" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§11_Item_78:_Synchronize_access_to_shared_mutable_data-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> Thread synchronization ensures that objects are modified by only one thread at a time and prevents threads from accessing partially updated objects during modification by another thread.<sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§11_Item_78:_Synchronize_access_to_shared_mutable_data_2-1" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§11_Item_78:_Synchronize_access_to_shared_mutable_data-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> The Java language has built-in constructs to support this coordination.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Processes_and_threads">Processes and threads</h2></div>
<p>Most implementations of the <a href="Java_virtual_machine" title="Java virtual machine">Java virtual machine</a> run as a single <a href="Process_(computing)" title="Process (computing)">process</a>. In the Java programming language, <a href="Concurrent_computing" title="Concurrent computing">concurrent programming</a> is primarily concerned with <a href="Thread_(computing)" title="Thread (computing)">threads</a> (also called <a href="Light-weight_process" title="Light-weight process">lightweight processes</a>). Multiple processes can only be realized with multiple JVMs.
</p>
<div class="mw-heading mw-heading3"><h3 id="Thread_objects">Thread objects</h3></div>
<p>Threads share the process' resources, including memory and open files. This makes for efficient, but potentially problematic, communication.<sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§11_Item_78:_Synchronize_access_to_shared_mutable_data_2-2" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§11_Item_78:_Synchronize_access_to_shared_mutable_data-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> Every application has at least one thread called the main thread. The main thread has the ability to create additional threads as <b><code><a rel="nofollow" class="external text" href="https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/Runnable.html">Runnable</a></code></b> or <b><code><a rel="nofollow" class="external text" href="https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/util/concurrent/Callable.html">Callable</a></code></b> objects. The <code>Callable</code> interface is similar to <code>Runnable</code> in that both are designed for classes whose instances are potentially executed by another thread.<sup id="cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer2006125–126§6.3.2_Result-bearing_tasks:_Callable_and_Future_3-0" class="reference"><a href="#cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer2006125–126§6.3.2_Result-bearing_tasks:_Callable_and_Future-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup> A <code>Runnable</code>, however, does not return a result and cannot throw a checked exception.<sup id="cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer200695–98§5.5.2_FutureTask_4-0" class="reference"><a href="#cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer200695–98§5.5.2_FutureTask-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup>
</p><p>Each thread can be scheduled<sup id="cite_ref-FOOTNOTEBloch2018336–337Chapter_§11_Item_84_Don't_depend_on_the_thread_scheduler_5-0" class="reference"><a href="#cite_note-FOOTNOTEBloch2018336–337Chapter_§11_Item_84_Don't_depend_on_the_thread_scheduler-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup> on a different <a href="Multi-core_processor" title="Multi-core processor">CPU core</a><sup id="cite_ref-FOOTNOTEBloch2018311Chapter_§11_Concurrency_6-0" class="reference"><a href="#cite_note-FOOTNOTEBloch2018311Chapter_§11_Concurrency-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> or use <a href="Preemption_(computing)#Time_slice" title="Preemption (computing)">time-slicing</a> on a single hardware processor, or time-slicing on many hardware processors. There is no general solution to how Java threads are mapped to native OS threads. Every JVM implementation can do this differently.
</p><p>Each thread is associated with an instance of the class <code>Thread</code>. Threads can be managed either by directly using the <code>Thread</code> objects, or indirectly by using abstract mechanisms such as <code>Executor</code>s or <code>Task</code>s.<sup id="cite_ref-FOOTNOTEBloch2018323–324Chapter_§5_Item_80:_Prefer_executors,_tasks,_and_streams_to_threads_7-0" class="reference"><a href="#cite_note-FOOTNOTEBloch2018323–324Chapter_§5_Item_80:_Prefer_executors,_tasks,_and_streams_to_threads-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading4"><h4 id="Starting_a_Thread">Starting a Thread</h4></div>
<p>Two ways to start a Thread:
</p>
<div class="mw-heading mw-heading5"><h5 id="Provide_a_runnable_object">Provide a runnable object</h5></div>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">HelloRunnable</span><span class="w"> </span><span class="kd">implements</span><span class="w"> </span><span class="n">Runnable</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nd">@Override</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">run</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"Hello from thread!"</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span><span class="w"> </span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="p">(</span><span class="k">new</span><span class="w"> </span><span class="n">Thread</span><span class="p">(</span><span class="k">new</span><span class="w"> </span><span class="n">HelloRunnable</span><span class="p">())).</span><span class="na">start</span><span class="p">();</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading5"><h5 id="Subclass_thread">Subclass thread</h5></div>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">HelloThread</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">Thread</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nd">@Override</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">run</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"Hello from thread!"</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span><span class="w"> </span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="p">(</span><span class="k">new</span><span class="w"> </span><span class="n">HelloThread</span><span class="p">()).</span><span class="na">start</span><span class="p">();</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading4"><h4 id="Interrupts">Interrupts</h4></div>
<p>An interrupt tells a thread that it should stop what it is doing and do something else. A thread sends an interrupt by invoking <code><a rel="nofollow" class="external text" href="https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/Thread.html#interrupt()">interrupt()</a></code> on the <code>Thread</code> object for the thread to be interrupted. The interrupt mechanism is implemented using an internal <code>boolean</code> flag known as the "interrupted status".<sup id="cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer2006138–141§7.1.1_Interruption_8-0" class="reference"><a href="#cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer2006138–141§7.1.1_Interruption-8"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup> Invoking <code>interrupt()</code> sets this flag.<sup id="cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer200692–94§5.4_Blocking_and_interruptible_methods_9-0" class="reference"><a href="#cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer200692–94§5.4_Blocking_and_interruptible_methods-9"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup> By convention, any method that exits by throwing an <code>InterruptedException</code> clears the interrupted status when it does so. However, it's always possible that the interrupted status will immediately be set again, by another thread invoking <code>interrupt()</code>.
</p>
<div class="mw-heading mw-heading4"><h4 id="Joins">Joins</h4></div>
<p>The
<code><a rel="nofollow" class="external text" href="https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/Thread.html#join()">java.lang.Thread#join()</a></code> method allows one <code>Thread</code> to wait for the completion of another.
</p>
<div class="mw-heading mw-heading4"><h4 id="Exceptions">Exceptions</h4></div>
<p>Uncaught exceptions thrown by code will terminate the thread. The main thread prints exceptions to the console, but user-created threads need a handler registered to do so.<sup id="cite_ref-10" class="reference"><a href="#cite_note-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-11" class="reference"><a href="#cite_note-11"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="Memory_model">Memory model</h2></div>
<p>The <a href="Java_memory_model" title="Java memory model">Java memory model</a> describes how threads in the Java programming language interact through memory. On modern platforms, code is frequently not executed in the order it was written. It is reordered by the <a href="Compiler" title="Compiler">compiler</a>, the <a href="Central_processing_unit" title="Central processing unit">processor</a> and the <a href="Memory_hierarchy" title="Memory hierarchy">memory subsystem</a> to achieve maximum performance. The Java programming language does not guarantee <a href="Linearizability" title="Linearizability">linearizability</a>, or even <a href="Sequential_consistency" title="Sequential consistency">sequential consistency</a>,<sup id="cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer2006338–339§16.1.1_Platform_memory_models_12-0" class="reference"><a href="#cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer2006338–339§16.1.1_Platform_memory_models-12"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup> when reading or writing fields of shared objects, and this is to allow for <a href="Compiler_optimization" class="mw-redirect" title="Compiler optimization">compiler optimizations</a> (such as <a href="Register_allocation" title="Register allocation">register allocation</a>, <a href="Common_subexpression_elimination" title="Common subexpression elimination">common subexpression elimination</a>, and <a href="Partial_redundancy_elimination" class="mw-redirect" title="Partial redundancy elimination">redundant read elimination</a>) all of which work by reordering memory reads—writes.<sup id="cite_ref-13" class="reference"><a href="#cite_note-13"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Synchronization">Synchronization</h3></div>
<p>Threads communicate primarily by sharing access to fields and the objects that reference fields refer to. This form of communication is extremely efficient, but makes two kinds of errors possible: thread interference and memory consistency errors. The tool needed to prevent these errors is synchronization.
</p><p><i>Reorderings</i> can come into play in incorrectly <a href="Synchronization_(computer_science)" title="Synchronization (computer science)">synchronized</a> <a href="Multithreading_(software)" class="mw-redirect" title="Multithreading (software)">multithreaded</a> programs, where one thread is able to observe the effects of other threads, and may be able to detect that variable accesses become visible to other threads in a different order than executed or specified in the program.
Most of the time, one thread doesn't care what the other is doing. But when it does, that's what synchronization is for.
</p><p>To synchronize threads, Java uses <a href="Monitor_(synchronization)" title="Monitor (synchronization)">monitors</a>, which are a high-level mechanism for allowing only one thread at a time to execute a region of code protected by the monitor. The behavior of monitors is explained in terms of <a href="Lock_(computer_science)" title="Lock (computer science)">locks</a>; there is a lock associated with each object.
</p><p>Synchronization has several aspects. The most well-understood is <a href="Mutual_exclusion" title="Mutual exclusion">mutual exclusion</a>—only one thread can hold a monitor at once, so synchronizing on a monitor means that once one thread enters a synchronized block protected by a monitor, no other thread can enter a block protected by that monitor until the first thread exits the synchronized block.<sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§11_Item_78:_Synchronize_access_to_shared_mutable_data_2-3" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§11_Item_78:_Synchronize_access_to_shared_mutable_data-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>
</p><p>But there is more to synchronization than mutual exclusion. Synchronization ensures that memory writes by a thread before or during a synchronized block are made visible in a predictable manner to other threads which synchronize on the same monitor. After we exit a synchronized block, we release the monitor, which has the effect of flushing the cache to main memory, so that writes made by this thread can be visible to other threads. Before we can enter a synchronized block, we acquire the monitor, which has the effect of invalidating the local processor cache so that variables will be reloaded from main memory. We will then be able to see all of the writes made visible by the previous release.
</p><p>Reads—writes to fields are <a href="Linearizability" title="Linearizability">linearizable</a> if either the field is <a href="Volatile_variable" class="mw-redirect" title="Volatile variable">volatile</a>, or the field is protected by a unique <a href="Lock_(computer_science)" title="Lock (computer science)">lock</a> which is acquired by all readers and writers.
</p>
<div class="mw-heading mw-heading4"><h4 id="Locks_and_synchronized_blocks">Locks and synchronized blocks</h4></div>
<p>A thread can achieve mutual exclusion either by entering a synchronized block or method, which acquires an implicit lock,<sup id="cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer200625–26§2.3.1_Intrinsic_locks_14-0" class="reference"><a href="#cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer200625–26§2.3.1_Intrinsic_locks-14"><span class="cite-bracket">[</span>14<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-FOOTNOTEBloch2018126–129Chapter_§11_Item_78:_Synchronize_access_to_shared_mutable_data_2-4" class="reference"><a href="#cite_note-FOOTNOTEBloch2018126–129Chapter_§11_Item_78:_Synchronize_access_to_shared_mutable_data-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> or by acquiring an explicit lock (such as the <code>ReentrantLock</code> from the <code><a rel="nofollow" class="external text" href="https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/util/concurrent/locks/package-summary.html">java.util.concurrent.locks</a></code> package <sup id="cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer2006277–278§13_Explicit_Locks_15-0" class="reference"><a href="#cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer2006277–278§13_Explicit_Locks-15"><span class="cite-bracket">[</span>15<span class="cite-bracket">]</span></a></sup>). Both approaches have the same implications for memory behavior. If all accesses to a particular field are protected by the same lock, then reads—writes to that field are <a href="Linearizability" title="Linearizability">linearizable</a> (atomic).
</p>
<div class="mw-heading mw-heading4"><h4 id="Volatile_fields">Volatile fields</h4></div>
<p>When applied to a field, the Java <b><code>volatile</code></b> keyword guarantees that:
</p>
<ol><li><i>(In all versions of Java)</i> There is a global ordering on the reads and writes to a <b><code>volatile</code></b> variable. This implies that every <a href="Thread_(computer_science)" class="mw-redirect" title="Thread (computer science)">thread</a> accessing a <b><code>volatile</code></b> field will read its current value before continuing, instead of (potentially) using a cached value. (However, there is no guarantee about the relative ordering of volatile reads and writes with regular reads and writes, meaning that it's generally not a useful threading construct.)</li>
<li><i>(In Java 5 or later)</i> Volatile reads and writes establish a <a href="Happened-before" title="Happened-before">happens-before relationship</a>, much like acquiring and releasing a mutex.<sup id="cite_ref-16" class="reference"><a href="#cite_note-16"><span class="cite-bracket">[</span>16<span class="cite-bracket">]</span></a></sup> This relationship is simply a guarantee that memory writes by one specific statement are visible to another specific statement.</li></ol>
<p>A <b><code>volatile</code></b> fields are linearizable. Reading a <b><code>volatile</code></b> field is like acquiring a lock: the working memory is invalidated and the <b><code>volatile</code></b> field's current value is reread from memory. Writing a <b><code>volatile</code></b> field is like releasing a lock: the <b><code>volatile</code></b> field is immediately written back to memory.
</p>
<div class="mw-heading mw-heading4"><h4 id="Final_fields">Final fields</h4></div>
<p>A field declared to be <b><code>final</code></b> cannot be modified once it has been initialized.<sup id="cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer200648§3.4.1_Final_fields_17-0" class="reference"><a href="#cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer200648§3.4.1_Final_fields-17"><span class="cite-bracket">[</span>17<span class="cite-bracket">]</span></a></sup> An object's <b><code>final</code></b> fields are initialized in its constructor. As long as the <b><code>this</code></b> reference is not released from the constructor before the constructor returns, then the correct value of any <b><code>final</code></b> fields will be visible to other threads without synchronization.<sup id="cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer200641–42§3.2.1_Safe_construction_practices_18-0" class="reference"><a href="#cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer200641–42§3.2.1_Safe_construction_practices-18"><span class="cite-bracket">[</span>18<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="History">History</h2></div>
<p>Since <a href="J2SE_1.2" class="mw-redirect" title="J2SE 1.2">JDK 1.2</a>, Java has included a standard set of collection classes, the <a href="Java_collections_framework" title="Java collections framework">Java collections framework</a>
</p><p><a href="Doug_Lea" title="Doug Lea">Doug Lea</a>, who also participated in the Java collections framework implementation, developed a concurrency <a href="Java_package" title="Java package">package</a>, comprising several concurrency primitives and a large battery of collection-related classes.<sup id="cite_ref-douglea_19-0" class="reference"><a href="#cite_note-douglea-19"><span class="cite-bracket">[</span>19<span class="cite-bracket">]</span></a></sup> This work was continued and updated as part of <a href="Java_Community_Process" title="Java Community Process">JSR</a> 166 which was chaired by Doug Lea.
</p><p><a href="J2SE_5.0" class="mw-redirect" title="J2SE 5.0">JDK 5.0</a> incorporated many additions and clarifications to the Java concurrency model. The concurrency APIs developed by JSR 166 were also included as part of the JDK for the first time. <a href="Java_Memory_Model" class="mw-redirect" title="Java Memory Model">JSR 133</a> provided support for well-defined atomic operations in a multithreaded/multiprocessor environment.
</p><p>Both the <a href="Java_SE_6" class="mw-redirect" title="Java SE 6">Java SE 6</a> and <a href="Java_SE_7" class="mw-redirect" title="Java SE 7">Java SE 7</a> releases introduced updated versions of the JSR 166 APIs as well as several new additional APIs.
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Concurrency_(computer_science)" title="Concurrency (computer science)">Concurrency (computer science)</a></li>
<li><a href="Concurrency_pattern" title="Concurrency pattern">Concurrency pattern</a></li>
<li><a href="Fork%E2%80%93join_model" title="Fork–join model">Fork–join model</a></li>
<li><a href="Memory_barrier" title="Memory barrier">Memory barrier</a></li>
<li><a href="Memory_model_(programming)" title="Memory model (programming)">Memory models</a></li>
<li><a href="Thread_safety" title="Thread safety">Thread safety</a></li>
<li><a href="ThreadSafe" title="ThreadSafe">ThreadSafe</a></li>
<li><a href="Java_ConcurrentMap" title="Java ConcurrentMap">Java ConcurrentMap</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="Notes">Notes</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */
.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}
/* end https://en.wikipedia.org/ */
</style><div class="reflist reflist-columns references-column-width" style="column-width: 30em;">
<ol class="references">
<li id="cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer200615–17§2_Thread_Safety-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer200615–17§2_Thread_Safety_1-0">^</a></b></span> <span class="reference-text"><a href="#CITEREFGoetzPeierlsBlochBowbeer2006">Goetz et al. 2006</a>, pp. 15–17, §2 Thread Safety.</span>
</li>
<li id="cite_note-FOOTNOTEBloch2018126–129Chapter_§11_Item_78:_Synchronize_access_to_shared_mutable_data-2"><span class="mw-cite-backlink">^ <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§11_Item_78:_Synchronize_access_to_shared_mutable_data_2-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§11_Item_78:_Synchronize_access_to_shared_mutable_data_2-1"><sup><i><b>b</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§11_Item_78:_Synchronize_access_to_shared_mutable_data_2-2"><sup><i><b>c</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§11_Item_78:_Synchronize_access_to_shared_mutable_data_2-3"><sup><i><b>d</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBloch2018126–129Chapter_§11_Item_78:_Synchronize_access_to_shared_mutable_data_2-4"><sup><i><b>e</b></i></sup></a></span> <span class="reference-text"><a href="#CITEREFBloch2018">Bloch 2018</a>, pp. 126–129, Chapter §11 Item 78: Synchronize access to shared mutable data.</span>
</li>
<li id="cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer2006125–126§6.3.2_Result-bearing_tasks:_Callable_and_Future-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer2006125–126§6.3.2_Result-bearing_tasks:_Callable_and_Future_3-0">^</a></b></span> <span class="reference-text"><a href="#CITEREFGoetzPeierlsBlochBowbeer2006">Goetz et al. 2006</a>, pp. 125–126, §6.3.2 Result-bearing tasks: Callable and Future.</span>
</li>
<li id="cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer200695–98§5.5.2_FutureTask-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer200695–98§5.5.2_FutureTask_4-0">^</a></b></span> <span class="reference-text"><a href="#CITEREFGoetzPeierlsBlochBowbeer2006">Goetz et al. 2006</a>, pp. 95–98, §5.5.2 FutureTask.</span>
</li>
<li id="cite_note-FOOTNOTEBloch2018336–337Chapter_§11_Item_84_Don't_depend_on_the_thread_scheduler-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-FOOTNOTEBloch2018336–337Chapter_§11_Item_84_Don't_depend_on_the_thread_scheduler_5-0">^</a></b></span> <span class="reference-text"><a href="#CITEREFBloch2018">Bloch 2018</a>, pp. 336–337, Chapter §11 Item 84 Don't depend on the thread scheduler.</span>
</li>
<li id="cite_note-FOOTNOTEBloch2018311Chapter_§11_Concurrency-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-FOOTNOTEBloch2018311Chapter_§11_Concurrency_6-0">^</a></b></span> <span class="reference-text"><a href="#CITEREFBloch2018">Bloch 2018</a>, p. 311, Chapter §11 Concurrency.</span>
</li>
<li id="cite_note-FOOTNOTEBloch2018323–324Chapter_§5_Item_80:_Prefer_executors,_tasks,_and_streams_to_threads-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-FOOTNOTEBloch2018323–324Chapter_§5_Item_80:_Prefer_executors,_tasks,_and_streams_to_threads_7-0">^</a></b></span> <span class="reference-text"><a href="#CITEREFBloch2018">Bloch 2018</a>, pp. 323–324, Chapter §5 Item 80: Prefer executors, tasks, and streams to threads.</span>
</li>
<li id="cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer2006138–141§7.1.1_Interruption-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer2006138–141§7.1.1_Interruption_8-0">^</a></b></span> <span class="reference-text"><a href="#CITEREFGoetzPeierlsBlochBowbeer2006">Goetz et al. 2006</a>, pp. 138–141, §7.1.1 Interruption.</span>
</li>
<li id="cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer200692–94§5.4_Blocking_and_interruptible_methods-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer200692–94§5.4_Blocking_and_interruptible_methods_9-0">^</a></b></span> <span class="reference-text"><a href="#CITEREFGoetzPeierlsBlochBowbeer2006">Goetz et al. 2006</a>, pp. 92–94, §5.4 Blocking and interruptible methods.</span>
</li>
<li id="cite_note-10"><span class="mw-cite-backlink"><b><a href="#cite_ref-10">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite id="CITEREFOracle" class="citation web cs1">Oracle. <a rel="nofollow" class="external text" href="http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.html">"Interface Thread.UncaughtExceptionHandler"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">10 May</span> 2014</span>.</cite></span>
</li>
<li id="cite_note-11"><span class="mw-cite-backlink"><b><a href="#cite_ref-11">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://literatejava.com/threading/silent-thread-death-unhandled-exceptions/">"Silent Thread death from unhandled exceptions"</a>. <i>literatejava.com</i>. 10 May 2014<span class="reference-accessdate">. Retrieved <span class="nowrap">10 May</span> 2014</span>.</cite></span>
</li>
<li id="cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer2006338–339§16.1.1_Platform_memory_models-12"><span class="mw-cite-backlink"><b><a href="#cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer2006338–339§16.1.1_Platform_memory_models_12-0">^</a></b></span> <span class="reference-text"><a href="#CITEREFGoetzPeierlsBlochBowbeer2006">Goetz et al. 2006</a>, pp. 338–339, §16.1.1 Platform memory models.</span>
</li>
<li id="cite_note-13"><span class="mw-cite-backlink"><b><a href="#cite_ref-13">^</a></b></span> <span class="reference-text">Herlihy, Maurice, and Nir Shavit. "The art of multiprocessor programming." PODC. Vol. 6. 2006.</span>
</li>
<li id="cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer200625–26§2.3.1_Intrinsic_locks-14"><span class="mw-cite-backlink"><b><a href="#cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer200625–26§2.3.1_Intrinsic_locks_14-0">^</a></b></span> <span class="reference-text"><a href="#CITEREFGoetzPeierlsBlochBowbeer2006">Goetz et al. 2006</a>, pp. 25–26, §2.3.1 Intrinsic locks.</span>
</li>
<li id="cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer2006277–278§13_Explicit_Locks-15"><span class="mw-cite-backlink"><b><a href="#cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer2006277–278§13_Explicit_Locks_15-0">^</a></b></span> <span class="reference-text"><a href="#CITEREFGoetzPeierlsBlochBowbeer2006">Goetz et al. 2006</a>, pp. 277–278, §13 Explicit Locks.</span>
</li>
<li id="cite_note-16"><span class="mw-cite-backlink"><b><a href="#cite_ref-16">^</a></b></span> <span class="reference-text">
Section 17.4.4: Synchronization Order
<cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4.4">"The Java® Language Specification, Java SE 7 Edition"</a>. <a href="Oracle_Corporation" title="Oracle Corporation">Oracle Corporation</a>. 2013<span class="reference-accessdate">. Retrieved <span class="nowrap">2013-05-12</span></span>.</cite></span>
</li>
<li id="cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer200648§3.4.1_Final_fields-17"><span class="mw-cite-backlink"><b><a href="#cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer200648§3.4.1_Final_fields_17-0">^</a></b></span> <span class="reference-text"><a href="#CITEREFGoetzPeierlsBlochBowbeer2006">Goetz et al. 2006</a>, p. 48, §3.4.1 Final fields.</span>
</li>
<li id="cite_note-FOOTNOTEGoetzPeierlsBlochBowbeer200641–42§3.2.1_Safe_construction_practices-18"><span class="mw-cite-backlink"><b><a href="#cite_ref-FOOTNOTEGoetzPeierlsBlochBowbeer200641–42§3.2.1_Safe_construction_practices_18-0">^</a></b></span> <span class="reference-text"><a href="#CITEREFGoetzPeierlsBlochBowbeer2006">Goetz et al. 2006</a>, pp. 41–42, §3.2.1 Safe construction practices.</span>
</li>
<li id="cite_note-douglea-19"><span class="mw-cite-backlink"><b><a href="#cite_ref-douglea_19-0">^</a></b></span> <span class="reference-text"><cite id="CITEREFDoug_Lea" class="citation web cs1"><a href="Doug_Lea" title="Doug Lea">Doug Lea</a>. <a rel="nofollow" class="external text" href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html">"Overview of package util.concurrent Release 1.3.4"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">2011-01-01</span></span>. <q><i>Note: Upon release of J2SE 5.0, this package enters maintenance mode: Only essential corrections will be released. J2SE5 package java.util.concurrent includes improved, more efficient, standardized versions of the main components in this package.</i></q></cite></span>
</li>
</ol></div>
<div class="mw-heading mw-heading2"><h2 id="Bibliography">Bibliography</h2></div>
<ul><li><cite id="CITEREFBloch2018" class="citation book cs1">Bloch, Joshua (2018). <i>"Effective Java: Programming Language Guide"</i> (third ed.). Addison-Wesley. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-0134685991</bdi>.</cite></li>
<li><cite id="CITEREFGoetzPeierlsBlochBowbeer2006" class="citation book cs1">Goetz, Brian; Peierls, Tim; Bloch, Joshua; Bowbeer, Joseph; Holmes, David; Lea, Doug (2006). <a rel="nofollow" class="external text" href="https://archive.org/details/javaconcurrencyi00goet"><i>Java Concurrency in Practice</i></a>. Addison Wesley. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>0-321-34960-1</bdi>.</cite></li>
<li><cite id="CITEREFLea1999" class="citation book cs1">Lea, Doug (1999). <a rel="nofollow" class="external text" href="https://archive.org/details/concurrentprogra00lead_0"><i>Concurrent Programming in Java: Design Principles and Patterns</i></a>. Addison Wesley. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>0-201-31009-0</bdi>.</cite></li></ul>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<ul><li><a rel="nofollow" class="external text" href="http://docs.oracle.com/javase/tutorial/essential/concurrency/">Oracle's Java concurrency tutorial</a></li>
<li><a rel="nofollow" class="external text" href="http://www.cs.umd.edu/~pugh/java/memoryModel/">William Pugh's Java memory model page</a></li>
<li><a rel="nofollow" class="external text" href="http://tutorials.jenkov.com/java-concurrency/index.html">Java Concurrency Tutorial by Jakob Jenkov</a></li>
<li><a rel="nofollow" class="external text" href="https://sourceforge.net/projects/javaconcurrenta">Java Concurrency Animations by Victor Grazi</a></li>
<li><a rel="nofollow" class="external text" href="https://web.archive.org/web/20181017222458/http://thread-safe.org/">Thread safety checker for Java classes</a></li></ul></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-04-30" href="https://en.wikipedia.org/wiki/?title=Java_concurrency&oldid=1288096876">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>